import time
from selenium.webdriver.common.by import By
from common.Utils import QE_Driver

#测试问卷考试系统注册页面

class QE_RegisterTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver = QE_Driver.driver
        self.driver.get(self.url)

    #注册成功的测试用例
    def RegisterSuccessTest(self):

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))

        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()
        time.sleep(1)
        #如果能跳转到登录页面，说明注册成功，否则注册失败
        #由于用户名已被注册，所以注册失败，手动跳转
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(7) > a").click()
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span")
        # 添加屏幕截图
        QE_Driver.getScreenshot()

##########################################################################################################################

    # 注册失败的测试用例

    def RegisterFailTest(self):

        # 测试用例一：不输入用户名、但输入登陆账号、密码、确认密码后进行注册

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))

        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()
        time.sleep(1)
        #检查是否注册失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#name_help > div").text
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "用户名是必填项！"

##########################################################################################################################

        #测试用例二：输入了已经存在的账号进行注册

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))

        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()
        time.sleep(1)
        #检查是否注册失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div").text
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "账号已存在"

##########################################################################################################################

        # 测试用例三：密码和密码确认时输入的不一致

        #清空输入框
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        QE_Driver.clear(self.driver.find_element(By.CSS_SELECTOR, "#rePassword"))

        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("123123")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()
        time.sleep(1)
        #检查是否注册失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#rePassword_help > div").text
        #添加屏幕截图
        QE_Driver.getScreenshot()
        #断言检测一下是否符合预期
        assert actual == "两次密码不一致!"


#测试
# register = QE_RegisterTest()
# register.RegisterFailTest()
# register.RegisterSuccessTest()

# 指定浏览器的退出
# QE_Driver.driver.quit()
